"use strict";

import {
    //核心
    React, Enhance, Controller, $,
    tools,
    // 组件
    Table, TabPage, Lang, Dialog, Tree,
    Form, Page, Upload, Icon, Grid, Panel,
    Input, BoxWidget, Button, Lock, Directive
} from 'admin';


var {http} = tools;
import valid from '../../validate/admin.json';

class VisitorController extends Controller {
    state = {
        tableProps: null,
        // sex: [
        //     {title: '女', value: 0},
        //     {title: '男', value: 1}
        // ],
        // edit: {}
    }

    constructor() {
        super();

        this.state.tableProps = {
            update: {},
            query: {},
            // types: {
            //     state: function (data, options) {
            //         // debugger;
            //         if (data.val == 1)
            //             return <span className='label bg-red'>男</span>;
            //         if (data.val == 0)
            //             return <span className='label bg-green'>女</span>;
            //
            //     }
            // },
            // buttons: {
            //     edit: {
            //         onClick: this._handlerEditDialog
            //     },
            //     upload: this._handlerCreateUpload,
            //     'delete': {
            //         onClick: this._handlerDeleteDialog
            //     }
            // },
            pk: 'id',
            fields: [
                {
                    order: 0,
                    name: 'id',
                    width: 50,
                },
                {
                    order: 0,
                    name: 'name',
                },
                {
                    order: 0,
                    name: 'phone',
                },
                {
                    order: 0,
                    name: 'work_unit',
                },
                {
                    order: 0,
                    name: 'floor',
                },
                {
                    order: 0,
                    name: 'v_time',
                    type: {
                        name: 'datetime'
                    }
                },
                {
                    order: 0,
                    name: 'user_name',
                },
                {
                    order: 0,
                    name: 'date',
                    width: 200,
                },
                {
                    order: 0,
                    name: 'time',

                },

            ]
        };
    }

    render() {
        var {edit} = this.state;
        return (
            <Grid>
                <Grid col="12 12 12 12">
                    <BoxWidget
                        elem={Form}
                        onSubmit={this._handlerSearch.bind(this)}
                        color="4" buttons={[{icon:'search',title:'search',type:'submit'}]}
                        className="no-padding"
                    >
                        <Grid col="2 2 2 2"><Input type="text" name="name" message={this._lang('name')}/></Grid>
                        <Grid col="2 2 2 2"><Input type="text" name="phone" message={this._lang('phone')}/></Grid>
                    </BoxWidget>
                    <BoxWidget color="3">
                        <Lock>
                            <Table ref="table" {...this.props} {...this.state.tableProps}></Table>
                        </Lock>
                    </BoxWidget>
                </Grid>

            </Grid>
        );
    }

    /**
     *
     * @private
     */
    _handlerAddDialog() {
        this.refs.addDialog.show();
    }

    /**
     *
     * @param edit
     * @private
     */
    _handlerEditDialog(edit) {
        this.setState({edit}, (function () {
            this.refs.editDialog.show();
        }).bind(this));
    }

    _handlerEditSuccess(data) {
        if (data) {
            this.refs.table.update(data);
            this.refs.editDialog.hide();
        }
    }

    /**
     *
     * @private
     */
    _handlerAddSuccess(data) {
        if (data) {
            this.refs.table.refresh();
            this.refs.addDialog.hide();
        }
    }

    /**
     *
     * @param key
     * @param data
     * @param index
     * @returns {XML}
     * @private
     */
    _handlerCreateUpload(key, data, index) {
        return (
            <Upload
                url={window.SERVER_URL+"/scdc/upload/get_header"}
                onSuccess={this._handlerUploadSuccess}
                post={{id:data.id}}
            ><Icon icon="upload" type="admin"></Icon>
            </Upload>
        );
    }

    /**
     *
     * @param data
     * @private
     */
    _handlerUploadSuccess(data) {
        this.refs.table.update(data);
    }


    /**
     *
     * @param data
     * @returns {boolean}
     * @private
     */
    _handlerSearch(data) {
        this.refs.table.query(data);
        return false;
    }

    /**
     *
     * @param delete
     * @private
     */
    _handlerDeleteDialog(edit) {
        this.setState({edit}, (function () {
            this.refs.deleteDialog.show();
        }).bind(this));
    }

    /**
     *
     * @param data
     * @private
     */
    _handlerDeleteSuccess(data) {
        if (data) {
            this.refs.table.refresh(data);
            this.refs.deleteDialog.hide();
            window.toast(<Lang lang="message.success.delete"/>);
        } else {
            window.toast(<Lang lang="message.error.delete"/>);
        }
    }
}

export default Enhance(VisitorController);